package com.hit.server.biz.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hit.common.core.util.HitConvertUtils;
import com.hit.common.core.util.R;
import com.hit.common.log.annotation.SysLog;
import com.hit.server.api.entity.CheckStrategyEntity;
import com.hit.server.api.dto.CheckStrategyQueryDTO;
import com.hit.server.api.dto.CheckStrategySaveDTO;
import com.hit.server.api.dto.CheckStrategyUpdateDTO;
import com.hit.server.api.vo.CheckStrategyVO;
import com.hit.server.api.service.CheckStrategyService;
import com.hit.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 检测策略
 *
 * @author kehanjiang
 * @date 2025-04-16 11:16:29
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/checkStrategy" )
@Tag(description = "checkStrategy" , name = "检测策略管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CheckStrategyController {

    private final  CheckStrategyService checkStrategyService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param dto 检测策略
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    public R<Page<CheckStrategyVO>> getCheckStrategyPage(@ParameterObject Page page, @ParameterObject CheckStrategyQueryDTO dto) {
	    dto = HitConvertUtils.emptyToNull(dto);
        return R.ok(checkStrategyService.getCheckStrategyPage(page, dto));
    }


    /**
     * 通过id查询检测策略
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    public R<CheckStrategyVO> getById(@PathVariable("id" ) Long id) {
        return R.ok(checkStrategyService.getCheckStrategyById(id));
    }

    /**
     * 新增检测策略
     * @param dto 检测策略
     * @return R
     */
    @Operation(summary = "新增检测策略" , description = "新增检测策略" )
    @SysLog("新增检测策略" )
    @PostMapping
    public R<Boolean> save(@RequestBody CheckStrategySaveDTO dto) {
        return R.ok(checkStrategyService.saveCheckStrategy(dto));
    }

    /**
     * 修改检测策略
     * @param dto 检测策略
     * @return R
     */
    @Operation(summary = "修改检测策略" , description = "修改检测策略" )
    @SysLog("修改检测策略" )
    @PutMapping
    public R<Boolean> updateById(@RequestBody CheckStrategyUpdateDTO dto) {
        return R.ok(checkStrategyService.updateCheckStrategyById(dto));
    }

    /**
     * 通过id删除检测策略
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除检测策略" , description = "通过id删除检测策略" )
    @SysLog("通过id删除检测策略" )
    @DeleteMapping
    public R<Boolean> removeById(@RequestBody Long[] ids) {
        return R.ok(checkStrategyService.removeCheckStrategyBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param checkStrategy 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    public List<CheckStrategyEntity> export(CheckStrategyEntity checkStrategy,@RequestParam List<Long> ids) {
        return checkStrategyService.list(Wrappers.lambdaQuery(checkStrategy).in(ArrayUtil.isNotEmpty(ids), CheckStrategyEntity::getId, ids));
    }
}